home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / source_code / tydhtml / dyn.exe / CHAP13 / OldShellGame.java < prev    next >
Encoding:
Java Source  |  1997-06-24  |  3.6 KB  |  96 lines

  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.net.*;
  4. import java.applet.*;
  5.  
  6. public class OldShellGame extends Applet {
  7.     static int time=50;       // Number of timing loops between key frames 
  8.     static int numframes=8;   // Number of key frames in the animation
  9.     boolean running=false;    // Switch to turn animation on and off
  10.  
  11.     /**  The keyframe locations of images */
  12.     static int  movesX0[] = {0,0,0,284,0,142,284,142,142};
  13.     static int  movesY0[] = {50,50,50,176,50,176,50,176,50};
  14.     static int  movesX1[] = {142,142,142,142,284,0,0,0,284};
  15.     static int  movesY1[] = {50,50,50,176,50,176,50,176,50};
  16.     static int  movesX2[] = {284,126,284,0,142,284,142,284,0};
  17.     static int  movesY2[] = {50,176,50,176,50,176,50,176,50};
  18.     static int  movesX3[] = {142,142,300,16,158,300,158,300,16};
  19.     static int  movesY3[] = {192,192,66,192,66,192,66,192,66};
  20.  
  21.     /**  The current location of images */
  22.     int  left[] = {0,142,284,142};
  23.     int  top[] = {50,50,50,192};
  24.  
  25.     /** The images for the shell game */
  26.     Image shell;
  27.     Image stone;
  28.  
  29.     /** Initialize the applet. Resize and load images. */
  30.     public void init() {
  31.     stone = getImage(getCodeBase(), "gem.gif");
  32.     shell = getImage(getCodeBase(), "walnut.gif");
  33.     }
  34.  
  35.     /** Paint it. */
  36.     public void paint(Graphics g) {
  37.         int count = 0;
  38.         int curframe = 1;
  39.         Dimension d = size();
  40.         g.fillRect(0,0,d.width,d.height);
  41.         g.drawImage(stone, left[3], top[3], this);
  42.         g.drawImage(shell, left[0], top[0], this);
  43.         g.drawImage(shell, left[1], top[1], this);
  44.         g.drawImage(shell, left[2], top[2], this);
  45.         if(running) {
  46.           while(count < time*numframes) {
  47.             left[0] = movesX0[curframe-1] + 
  48.                ((movesX0[curframe]-movesX0[curframe-1])*(count % time)/time);
  49.             top[0]=movesY0[curframe-1] + 
  50.                ((movesY0[curframe]-movesY0[curframe-1])*(count % time)/time);
  51.             left[1]=movesX1[curframe-1] + 
  52.                ((movesX1[curframe]-movesX1[curframe-1])*(count % time)/time);
  53.             top[1]=movesY1[curframe-1] + 
  54.                ((movesY1[curframe]-movesY1[curframe-1])*(count % time)/time);
  55.             left[2]=movesX2[curframe-1] + 
  56.                ((movesX2[curframe]-movesX2[curframe-1])*(count % time)/time);
  57.             top[2]=movesY2[curframe-1] + 
  58.                ((movesY2[curframe]-movesY2[curframe-1])*(count % time)/time);
  59.             left[3]=movesX3[curframe-1] + 
  60.                ((movesX3[curframe]-movesX3[curframe-1])*(count % time)/time);
  61.             top[3]=movesY3[curframe-1] + 
  62.                ((movesY3[curframe]-movesY3[curframe-1])*(count % time)/time);
  63.             g.fillRect(0,0,d.width,d.height);
  64.             if(count<time || count>=time*numframes) {
  65.                      g.drawImage(stone, left[3], top[3], this);
  66.             }
  67.             g.drawImage(shell, left[0], top[0], this);
  68.             g.drawImage(shell, left[1], top[1], this);
  69.             g.drawImage(shell, left[2], top[2], this);
  70.             count++;
  71.             if(count%time==0 && curframe<numframes) {
  72.                        curframe++;
  73.             }
  74.             Delay(200,count);
  75.           }
  76.         }
  77.         running=false;
  78.     }
  79.  
  80.     /** The user has clicked in the applet. Start the animation  */
  81.     public boolean mouseUp(Event evt, int x, int y) {
  82.              running=true;
  83.              left[3] = 142;
  84.              top[3] = 192;
  85.              repaint();
  86.              return true;
  87.     }
  88.  
  89.     public void Delay(long d,int count) {
  90.              long x=0,y=0;
  91.              for(x=0;x<d;x++) {
  92.                       y++;
  93.              }
  94.     }
  95. }
  96.